package com.course.coursedesign2022.service;

import com.course.coursedesign2022.mapper.UserMapper;
import com.course.coursedesign2022.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class YdgnNote {
    @Autowired
    UserMapper userMapper;

    public UserInfo ydgnNote(Integer id){
        UserInfo userInfo = userMapper.getUserInfoById(id);
        Integer growscore = userInfo.getGrowscore();
        Integer scoretotal = userInfo.getScoretotal();
        Date ydLastTime = userInfo.getYdLastTime();
        Date now = new Date();
        if(ydLastTime == null){
            userInfo.setYdLastTime(now);
            userInfo.setGrowscore(growscore+2);
            userInfo.setScoretotal(scoretotal+2);
            System.out.println("胰岛积分+2");
            userMapper.updateUserInfo(userInfo);
            return userInfo;
        }
        long lastTimeMills = ydLastTime.getTime();
        long nowMills = now.getTime();

        long tag = (long)90*24*60*60*1000;
        long sub = nowMills - lastTimeMills;
        if(sub < tag){
            System.out.println("胰岛积分间隔不足三个月,无法获取");
            return userInfo;
        }else {
            userInfo.setGrowscore(growscore+2);
            userInfo.setScoretotal(scoretotal+2);
            System.out.println("胰岛积分+2");
        }
        userInfo.setYdLastTime(now);
        userMapper.updateUserInfo(userInfo);
        return userInfo;
    }
}
